package com.mytest.plugins.demo;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.widget.SimpleAdapter;

import com.getcapacitor.Bridge;
import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.ui.Toast;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

@NativePlugin
public class PluginsDemo extends Plugin {
//    public final startActive =
    // 特别重要
    public Activity getStarActive(){
        return getBridge().getActivity();
    }

    @PluginMethod
    public void echo(PluginCall call) {
        String value = call.getString("value");

        JSObject ret = new JSObject();
        ret.put("value", value);
        call.success(ret);
    }
    private Context getContent(){
        return getContext();
    }
    //  延迟退出App代码
    @PluginMethod
    public void exitApp(PluginCall call) {
        final PluginCall calls = call;
        try{
            final Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask(){
                public int times = 10;
                @Override
                public void run(){
                    Toast.show(getContent(), "还有" + times-- + "S退出App");
                    if(times  == 0) {
                        timer.cancel();
                        getBridge().getActivity().finish();
                    }
                }
            },1000,1000);
        }catch (Exception e) {
            call.reject(e.toString());
        }

    }
    // 打开其他App
    @PluginMethod
    public void openUrl(PluginCall call) {
       try {
           String packnName = call.getString("url");
           Intent intent = getStarActive().getPackageManager().getLaunchIntentForPackage(packnName);
           JSObject status = new JSObject();
           if (intent != null) {
               intent.putExtra("type", "110");
               intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               getStarActive().startActivity(intent);
               status.put("complete",true);
               call.resolve(status);
           }else {
               status.put("complete","packageName error, please check you packageName");
               call.reject(status.toString());
           }
       }catch (Exception e) {
           call.reject(e.toString());
       }
    }
    // 检查app是否存在
    @PluginMethod
    public void checkPackExits(PluginCall call) {
        JSObject reslut = new JSObject();
        try {
            String packName = call.getString("packName");
            PackageInfo packageInfo = null;
            try {
                packageInfo = getStarActive().getPackageManager().getPackageInfo(packName, 0);
                reslut.put("reslut",packageInfo != null);
                call.resolve(reslut);
            } catch (PackageManager.NameNotFoundException e) {
                call.reject(e.getMessage());
            }
        } catch (Exception e) {
           call.reject(e.getMessage());
        }
    }
    // 获取App已经安装列表
    @PluginMethod
    public void getAppList(PluginCall call){
        PackageManager pm = getStarActive().getPackageManager();
        List<PackageInfo> installedPackages = pm.getInstalledPackages(0);  // 获取所以已安装的包
        // 创建数组选项
        ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
        ArrayList<AppInfo> list = new ArrayList<AppInfo>();
        // 循环包
        for (PackageInfo packageInfo : installedPackages) {
            AppInfo info = new AppInfo();
            String packageName = packageInfo.packageName;  // 包名
            ApplicationInfo applicationInfo = packageInfo.applicationInfo;  // 应用信息
            String name = applicationInfo.loadLabel(pm).toString();  // 应用名称
            Drawable icon = applicationInfo.loadIcon(pm);  // 应用图标
            System.out.println("name=========="+name);
            System.out.println("packageName=========="+packageName);
            info.name = name;
            info.packageName = packageName;
            info.icon = icon;
            // 状态机,通过01状态来表示是否具备某些属性和功能
            int flags = applicationInfo.flags;  // 获取应用标记
            if ((flags & ApplicationInfo.FLAG_EXTERNAL_STORAGE) == ApplicationInfo
                    .FLAG_EXTERNAL_STORAGE) {
                //安装在sdcard
                info.isRom = false;

            } else {
                //安装在手机
                info.isRom = true;
            }

            if ((flags & ApplicationInfo.FLAG_SYSTEM) == ApplicationInfo
                    .FLAG_SYSTEM) {
                //系统应用
                info.isUser = false;

            } else {
                //用户应用
                info.isUser = true;
            }
            // push已经安装的包
            if (info.isUser) {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("ItemTitle", name);    //文字
                map.put("ItemImage", icon.toString());   //图片
                map.put("ItempackName",packageName); // 包名
                map.put("ItemApplicationInfo",applicationInfo.toString()); // 应用信息
                listItems.add(map);
            }
        }
        // 转换数据格式
        JSONArray jsonArray = new JSONArray(listItems);;
        JSObject ret = new JSObject();
        ret.put("result", jsonArray);
        call.success(ret);
    }
}

class AppInfo {
    public String name;
    public String packageName;
    public Drawable icon;
    public boolean isUser;
    public boolean isRom;
}